# include <stdio.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <sys/mman.h>
# include <strings.h>
# include <linux/input.h>
# include <stdlib.h>

int tx,ty,tp;
int flag=0;

// mplayer 放器  -slave命令模式  除杂   设置通道   起始点坐标 x最大240  窗口大小 mp4不行  文件名  &后台
char *mplayer = "mplayer -slave -quiet -input file=/pipe -geometry 0:0 -zoom -x 800 -y 480 ssjs.avi &";
char *kill = "killall -SIGKILL mplayer";  // 杀死mplayer
char *mkpp = "mkfifo /pipe";  //创建管道 根目录下
char *chpp = "chmod 777 /pipe";  //更改管道属性
char *play_up = "echo seek +5 > /pipe";  //快进5
char *play_down = "echo seek -5 > /pipe";  // 快退5
char *sound_up = "echo volume +10 > /pipe";  //音量+10
char *sound_down = "echo volume -10 > /pipe";  //音量+10
char *play_pause = "echo pause > /pipe";  //暂停 继续

int open_jugg(int i)
{
	//char *j = "oepn_parm";
	//printf("%s=%d\n",j,i);
	if (i< 0)
	{
		//printf("failure\n");
		return 0;
	}
	else
	{
		//printf("success\n");
		return 1;
	}
}

int touch_scan() //获取一次触摸板坐标
{
	int tsfd = open("/dev/input/event0",O_RDWR);
	printf("TOUCH=%d\n",open_jugg(tsfd));
	struct input_event ts;
	bzero(&ts,sizeof(ts));  //清空数据
	while(1)
	{
		read(tsfd,&ts,sizeof(ts));
		if(ts.type == EV_ABS)  //绝对坐标事件
		{
			if(ts.code == ABS_X)
			{
				tx = ts.value;
				flag++;
				// printf("flag=%d",flag);
			}
			if(ts.code == ABS_Y)
			{
				ty = ts.value;
				flag++;
				// printf("flag=%d",flag);
			}
		}
		
		if (ts.type == EV_KEY)   //压力
		{
			if(ts.code == BTN_TOUCH)
			{
				tp = ts.value;
				flag++;
			}
		}
		
		if (flag == 3)
			{
				flag=0;
				break;
			}			
	}
	return 0;
}


int main(void)
{
	system(mkpp);
	system(chpp);
	system(kill);
	
	// while(1)
	// {
		// system(mplayer);
		// sleep(0.1);	
		while(1)
		{
			touch_scan();
			printf("x=%d,y=%d,p=%d\n",tx,ty,tp);
					
			if(tx>350&&tx<450&&ty>380&&ty<480)  //关闭
			{
				printf("Centry Down kill\n");
				system(kill);
				//break;
			}
			
			if(tx>350&&tx<450&&ty>0&&ty<100)  //开启
			{
				printf("Centry Up star\n");
				system(mplayer);
				//break;
			}
			
			if(tx>0&&tx<100&&ty>0&&ty<100)  //快退
			{
				// system(mplayer);
				// sleep(0.1);
				printf("LeftUp play_up\n");
				system(play_down);	
				sleep(0.5);  //按键消抖
				//break;
			}
			
			if(tx>700&&tx<800&&ty>0&&ty<100)  //快进
			{
				// system(mplayer);
				// sleep(0.1);
				printf("RightUp play_down\n");
				system(play_up);
				sleep(0.5);
				//break;
			}
			
			if(tx>0&&tx<100&&ty>380&&ty<480)  //音量减
			{
				// system(mplayer);
				// sleep(0.1);
				printf("LeftDown sound_down\n");
				system(sound_down);
				//break;
			}
			
			if(tx>700&&tx<800&&ty>380&&ty<480)  //音量减
			{
				// system(mplayer);
				// sleep(0.1);
				printf("RightDown sound_up\n");
				system(sound_up);
				//break;
			}
			
			if(tx>0&&tx<100&&ty>190&&ty<290)  //暂停 继续
			{
				printf("Right Centry pause\n");
				system(play_pause);
				sleep(0.5);
				//break;
			}
			
		}
	
	// }
	return 0;
}